﻿using System.Linq;
using Stimulus360.Models.Data;
using Stimulus360.Repositories.Interfaces;

namespace Stimulus360.Repositories
{
    /// <summary>
    ///     This class is used for CRUD operations for the User Profile entity
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable", Justification="DataContext does not hold expensive resources. Will be garbage collected automatically.")]
    public class UserRepository : IUserRepository
    {
        /// <summary>
        ///     The Linq to SQL DataContext to interact with the Database.
        /// </summary>
        private  Stimulus360DataContext _dataContext = new Stimulus360DataContext();

        #region IUserProfileRepository Members

        /// <summary>
        ///     Method to retrieve the user entity for the given user name
        /// </summary>
        /// <param name="userName">
        ///     Username for which the entity is to be retrieved
        /// </param>
        /// <returns>
        ///     User entity for the given name
        /// </returns>
        public UserProfile FindUserByName(string userName)
        {
            return _dataContext.UserProfiles.FirstOrDefault(u => u.UserName == userName);
        }

        /// <summary>
        ///     Method to create a new user
        /// </summary>
        /// <param name="user">
        ///     User to be created
        /// </param>
        public void CreateUser(UserProfile user)
        {
            _dataContext.UserProfiles.InsertOnSubmit(user);
            Save();
        }

        /// <summary>
        ///     Method to submit all tracked changes to the DB
        /// </summary>
        public void UpdateUser()
        {
            Save();
        }

        /// <summary>
        ///     Method to update an edited user to the database
        /// </summary>
        /// <param name="user">
        ///     User to be Updated
        /// </param>
        public void UpdateUser(UserProfile user)
        {
            var interestDetail = from detail in _dataContext.UserInterests
                                 where detail.UserName == user.UserName
                                 select detail;

            foreach (UserInterest interest in interestDetail)
            {
                _dataContext.UserInterests.DeleteOnSubmit(interest);

            }
            Save();
            string userName = user.UserName;
            var userProfile = _dataContext.UserProfiles.FirstOrDefault(u => u.UserName == userName);

            userProfile.FirstName = user.FirstName;
            userProfile.LastName = user.LastName;
            userProfile.Bio = user.Bio;
            userProfile.DateLastUpdated = user.DateLastUpdated;
            userProfile.Email = user.Email;

            foreach (UserInterest interest in user.UserInterests)
            {
                UserInterest newInterest = new UserInterest();
                newInterest.CategoryId = interest.CategoryId;
                newInterest.Category = interest.Category;
                newInterest.UserName = userName;

                _dataContext.UserInterests.InsertOnSubmit(newInterest);
            }

            Save();
            //The user object is getting cached. Refresh the Data context.
            _dataContext = new Stimulus360DataContext();
        }

        /// <summary>
        ///     Method to save the tracked DataContext changes to the DB.
        /// </summary>
        private void Save()
        {
            _dataContext.SubmitChanges();
        }

        #endregion

    }

}
